package com.rent.car.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.rent.car.constast.SysConstast;
import com.rent.car.pojo.User;
import com.rent.car.service.LogInfoService;
import com.rent.car.service.UserService;
import com.rent.car.utils.WebUtils;
import com.rent.car.vo.LogInfoVo;
import com.rent.car.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

/**
 * 用户登陆控制器
 */
@Controller
@RequestMapping("login")
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private LogInfoService logInfoService;

    /**
     * 跳转到登陆页面
     */
    @RequestMapping("toLogin")
    public String toLogin() {
        return "system/main/login";
    }

    /**
     * 跳转到修改密码页面
     */
    @RequestMapping("changePwdPage")
    public String changePwdPage() {
        return "system/main/changePwd";
    }

    /**
     * 登陆方法
     *
     * @param userVo
     * @return
     */
    @RequestMapping("login")
    public String login(UserVo userVo, Model model) {
        String code = WebUtils.getHttpSession().getAttribute("code").toString();
        if (userVo.getCode().equals(code)) {
            User user = this.userService.login(userVo);
            System.out.println(user);
            if (null != user) {
                //放到session
                WebUtils.getHttpSession().setAttribute("user", user);

                //记录登陆日志 向sys_log_login里面插入数据
                LogInfoVo logInfoVo = new LogInfoVo();
                logInfoVo.setLogintime(new Date());
                logInfoVo.setLoginname(user.getRealname() + "-" + user.getLoginname());
                logInfoVo.setLoginip(WebUtils.getIp());
                logInfoService.addLogInfo(logInfoVo);

                return "system/main/index";
            } else {
                model.addAttribute("error", SysConstast.USER_LOGIN_ERROR_MSG);
                return "system/main/login";
            }
        } else {
            model.addAttribute("error", SysConstast.USER_LOGIN_CODE_ERROR_MSG);
            return "system/main/login";
        }
    }

    /**
     * 得到登陆验证码
     *
     * @param response
     * @param session
     * @throws IOException
     */
    @RequestMapping("getCode")
    public void getCode(HttpServletResponse response, HttpSession session) throws IOException {
        //定义图形验证码的长和宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36, 4, 5);
        session.setAttribute("code", lineCaptcha.getCode());
        ServletOutputStream outputStream = response.getOutputStream();
        ImageIO.write(lineCaptcha.getImage(), "JPEG", outputStream);
    }

    /**
     * 注销
     *
     * @param req
     * @param resp
     * @throws Exception
     */
    @RequestMapping("quit")
    public String quit(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        req.getSession().removeAttribute("user");
        return "system/main/login";
    }
}
